#! /usr/bin/env python
# process_images.py - Draws graphs on the images for lweamonit

import sys # sys.exit
import os # os.path.exists
import ConfigParser # ini -file parsing

# Define the class
class lweamonit:
	"""lweamonit image processing class"""

	source = ""
	destination = ""
	delete_originals_after_processing = False

	db_name = ""
	db_hostname = ""
	db_port = ""
	db_username = ""
	db_password = ""

	# Constructor
	def __init__(self):
		"""Start the application"""

		# Determine path to config, default is current folder
		configPath = "./config.ini"
		# If we've been given an argument, that's the path to config
		if len(sys.argv) > 1:
			configPath = sys.argv[1]

		print "Loading config from " + configPath

		# Get settings from config
		self.loadConfig(configPath)
		
		print "Processing images... "
		print self.source
		print self.destination
		print self.delete_originals_after_processing
		print self.db_name
		print self.db_hostname
		print self.db_port
		print self.db_username
		print self.db_password

	def loadConfig(self, configPath):
		"""Load config from ini file in given path and validate"""

		# Variable for error tracking
		error = False

		# Check that the config exists
		if os.path.exists(configPath)==False:
			print "Could not find config file at " + configPath
			sys.exit()

		# Create config parser to parse the ini file
		config = ConfigParser.RawConfigParser()
		
		# Read the .ini file
		config.read(configPath)

		# Read the values

		# Image section settings
		self.source = config.get('images', 'originals')
		self.destination = config.get('images', 'processed')
		self.delete_originals_after_processing = config.getboolean('images', 'delete_originals_after_processing')

		# Database settings
		self.db_hostname = config.get('database', 'hostname')
		self.db_port = config.getint('database', 'port')
		self.db_name = config.get('database', 'name')
		self.db_username = config.get('database', 'username')
		self.db_password = config.get('database', 'password')

		if len(self.source)==0:
			error = True
			print "Source not defined in config"
		elif os.path.exists(self.source)==False:
			error = True
			print "Source does not exist"

		if len(self.destination)==0:
			error = True
			print "Destination not defined in config"

                if len(self.db_hostname)==0:
                        error = True
                        print "Database hostname not defined in config"

                if self.db_port==0:
                        error = True
                        print "Database port not defined in config"

		if len(self.db_name)==0:
			error = True
			print "Database name not defined in config"

		if len(self.db_username)==0:
			error = True
			print "DB username not defined in config"

		# Password can be empty, we shouldn't check if password is empty
		#if len(self.db_password)==0:
		#	error = True
		#	print "DB password not defined in config"

		# If there was an error, exit now.
		if error==True:
			print "Validation failed, exiting..."
			sys.exit()

		


# Last, but not least, we create an instance of lweamonit, this line will do everything there is to do
processor = lweamonit()

